package fast_fourier_transform;

/**
 * author :  apurv verma
 * email  :  dapurv5@gmail.com
 */


public class Complex {
	public double x,y;
	
	public Complex(double x, double y){
		this.x=x;
		this.y=y;
	}
	
	public Complex() {
	}
	
	public static Complex toComplex(double d){
		Complex c=new Complex();
		c.x=d;
		c.y=0;
		return c;
	}

	public static Complex toComplex(Object o){
		Complex c=new Complex();
		if (o instanceof Complex){
			c=(Complex)o;
		}
		return c;
	}

	public void display(){
		StringBuffer s;
		if (y<0)
			s=new StringBuffer("");
		else
			s=new StringBuffer("+");
		System.out.print(x+""+s+y+"j");
	}
	
	double amp(){
		return Math.sqrt(x*x+y*y);
	}
	
	double arg(){
		double t=Math.atan(y/x);
		return  t;
	}

	public static Complex conjugate(Complex c){
		Complex conj=new Complex(c.x,-1.0*c.y);
		return conj;
	}
		
	public static Complex add(Complex c1,Complex c2){
		Complex sum=new Complex();
		sum.x=c1.x+c2.x;
		sum.y=c1.y+c2.y;
		return sum;
	}
	
	public static Complex add(double d, Complex c){
		Complex sum=new Complex();
		sum.x=c.x+d;
		sum.y=c.y;
		return sum;
	}
	
	public static Complex add(Complex c,double d){
		Complex sum=new Complex();
		sum.x=c.x+d;
		sum.y=c.y;
		return sum;
	}
	
	public static Complex subtract(Complex c1, Complex c2){
		Complex diff=new Complex();
		diff.x=c1.x-c2.x;
		diff.y=c1.y-c2.y;
		return diff;
	}
	
	public static Complex subtract(double d, Complex c2){
		Complex diff=new Complex();
		diff.x=d-c2.x;
		diff.y=c2.y;
		return diff;
	}
	
	public static Complex subtract(Complex c2,double d){
		Complex diff=new Complex();
		diff.x=c2.x-d;
		diff.y=c2.y;
		return diff;
	}
	
	public static Complex multiply(Complex c1, Complex c2){
		Complex product=new Complex();
		product.x=c1.x*c2.x-c1.y*c2.y;
		product.y=c1.x*c2.y+c1.y*c2.x;
		return product;
	}
	
	public static Complex multiply(double d, Complex c2){
		Complex product=new Complex();
		product.x=c2.x*d;
		product.y=c2.y*d;
		return product;
	}
	
	public static Complex multiply(Complex c2,double d){
		Complex product=new Complex();
		product.x=c2.x*d;
		product.y=c2.y*d;
		return product;
	}

	public static Complex divide(Complex c1, Complex c2){
		Complex c,d=new Complex();
		c=Complex.conjugate(c2);
		double magn=c2.amp();
		d=Complex.divide( Complex.multiply(c1, c),(magn*magn) );
		return d;
	}
	
	public static Complex divide(double d, Complex c2){
		Complex c,C=new Complex();
		c=Complex.conjugate(c2);
		C=Complex.multiply(d, c);
		return C;
	}
	
	public static Complex divide(Complex c1, double d){
		Complex C=new Complex();
		C=Complex.multiply(c1,1/d);
		return C;
	}

}
